********************************************************************************
* Predicting has pik
********************************************************************************

clear

preserve
insheet using "$build/Neighborhood_characteristics.csv", clear
rename unique_id_new unique_id
save "$build/Neighborhood_characteristics.dta", replace
restore 

use "$build/Evic_data_ready_with_instruments.dta", clear
rename unique_id  unique_id_new
rename unique_id_old unique_id
merge m:1 unique_id using "$build/Evic_ilch2000_2016_pvs_unique.dta"
rename unique_id unique_id_old
rename unique_id_new unique_id 
drop if _merge == 2
rename _merge evic_evicpvs_merge

merge  1:1 unique_id  using "$build/Neighborhood_characteristics.dta"
drop if _merge == 2
drop _merge 

merge  1:1 unique_id  using "$build/Evic_data_comb_for_analysis_merge.dta", keepusing(unique_id districtXyearXquarter noprior d_prose)
drop if _merge == 2
drop _merge 

duplicates drop pik case_number if pik != "", force

replace median_rent = median_rent / 1000
gen has_pik = pik != "" 

replace ad_damnum = ad_damnum / 10000

gen pred_fem = 1 - proportion_male
replace pred_fem = 1 - proportion_male_alt2 if pred_fem == . 
gen pred_fem_sq = pred_fem^2

global cluster_ols="judgeXyear "
global cluster_iv="judgeXyear "
global stem = ""
global instrument="loo_vc_ivsamp100"
global fe="districtXyearXquarter"
global controls "pred_fem pred_whi pred_bla pred_his  joint  d_prose ad_damnum  median_rent pct_below_povline "
global bin_controls " joint  d_prose evicted "
global cts_controls " pred_fem pred_whi pred_bla pred_his ad_damnum  median_rent pct_below_povline"

foreach var in pred_bla pred_his pred_whi {
	replace `var' = `var'_alt if `var' == . | `var'>1
	replace `var' = . if `var' > 1
	gen `var'_sq = `var'^2
}



********************************************************************************
*============= RUNNING REGRESSIONS OF COVARS ON PIK  ===========================
********************************************************************************

gen obscount = 0
gen outcome = ""
gen group = ""


eststo haspik1: areg has_pik evicted $controls    if iv_samp100 == 1 & problem_clean_indicator==0, r cluster($cluster_ols) ab($fe)
		local obs1: di %12.2gc e(N)
		estadd local obs "`obs1'", replace
		local j = 1
		foreach var of varlist $bin_controls evicted {
			foreach varval in 0 1 {
				foreach has_pik in 0 1 {
					if (`has_pik'==1) {
						unique pik if e(sample) & has_pik==`has_pik' & `var' == `varval'
					}
					if (`has_pik'==0) {
						unique unique_id if e(sample) & has_pik==`has_pik' & `var' == `varval'
					}
					replace obscount =r(sum) in `j'
					replace outcome = "PIK on Eviction" in `j'
					replace group = "Has PIK Table: has_pik=`has_pik' and `var' = `varval'" in `j'
					local j = `j' + 1
					di "`j'"
				}
			}
		}
		foreach var of varlist $cts_controls  {
				foreach has_pik in 0 1 {
					if (`has_pik'==1) {
						unique pik if e(sample) & has_pik==`has_pik' 
					}
					if (`has_pik'==0) {
						unique unique_id if e(sample) & has_pik==`has_pik'
					}
					replace obscount =r(sum) in `j'
					replace outcome = "PIK on Eviction" in `j'
					replace group = "Has PIK Table: has_pik=`has_pik' , `var'" in `j'
					local j = `j' + 1
					di "`j'"
				}
		}

eststo haspik2: areg has_pik loo_vc_ivsamp100 $controls   if iv_samp100 == 1 & problem_clean_indicator==0 , r cluster($cluster_ols) ab($fe)
	local obs1: di %12.2gc e(N)
	estadd local obs "`obs1'", replace
		foreach var of varlist $bin_controls {
			foreach varval in 0 1 {
				foreach has_pik in 0 1 {
					if (`has_pik'==1) {
						unique pik if e(sample) & has_pik==`has_pik' & `var' == `varval'
					}
					if (`has_pik'==0) {
						unique unique_id if e(sample) & has_pik==`has_pik' & `var' == `varval'
					}
					
					replace obscount =r(sum) in `j'
					replace outcome = "PIK on Stringency" in `j'
					replace group = "Has PIK Table: has_pik=`has_pik' and `var' = `varval'" in `j'
					local j = `j' + 1
					di "`j'"
				}
			}
		}
		foreach var of varlist $cts_controls loo_vc_ivsamp100 {
				foreach has_pik in 0 1 {
					if (`has_pik'==1) {
						unique pik if e(sample) & has_pik==`has_pik'
					}
					if (`has_pik'==0) {
						unique unique_id if e(sample) & has_pik==`has_pik'
					}
					replace obscount =r(sum) in `j'
					replace outcome = "PIK on Stringency" in `j'
					replace group = "Has PIK Table: has_pik=`has_pik' , `var'" in `j'
					local j = `j' + 1
					di "`j'"
				}
		}

	
*****************
*    Export     *
*****************

esttab  haspik1 haspik2 using "${Inputs}/Cook_census_matches.csv", ///
replace compress nodepvars wrap csv nonotes label  se(5) b(5) ///
eqlabels(none)  star(* 0.10 ** 0.05 *** 0.01) ///
mtitles("Evicted" "Judge Stringency") 


